package cre.tomcat7.ch10.startup;

import cre.tomcat7.ch10.core.SimpleContextConfig;
import cre.tomcat7.ch10.core.SimpleUserDatabaseRealm;
import cre.tomcat7.ch10.core.SimpleWrapper;
import org.apache.catalina.*;
import org.apache.catalina.connector.http.HttpConnector;
import org.apache.catalina.core.StandardContext;
import org.apache.catalina.deploy.LoginConfig;
import org.apache.catalina.deploy.SecurityCollection;
import org.apache.catalina.deploy.SecurityConstraint;
import org.apache.catalina.loader.WebappClassLoader;
import org.apache.catalina.loader.WebappLoader;
import org.apache.naming.resources.ProxyDirContext;

import static cre.tomcat7.common.Constants.CATALINA_BASE;

/**
 * 安全性
 * tomcat bootstrap
 *
 * @Author penglai
 * @Date 2017/1/4.
 */
public class Bootstrap2 {

    public static void main(String[] args) {
        HttpConnector connector = new HttpConnector();
        Container container = createContainer();
        try {
            connector.setContainer(container);
            connector.initialize();

            connector.start();
            ((Lifecycle) container).start();

            printResource(container.getLoader());
            System.in.read();
            ((Lifecycle) container).stop();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void printResource(Loader loader) {
        WebappClassLoader classLoader = (WebappClassLoader) loader.getClassLoader();
        System.out.println("Resources's docBase: " + ((ProxyDirContext) classLoader.getResources()).getDocBase());

        String[] repositories = classLoader.findRepositories();
        for (String repository : repositories) {
            System.out.println(" repository: " + repository);
        }
    }

    private static Container createContainer() {
        System.setProperty("catalina.base", CATALINA_BASE);
        StandardContext container = new StandardContext();
        container.setPath("/myApp");
        container.setDocBase("myApp");

        SimpleWrapper wrapper1 = new SimpleWrapper();
        wrapper1.setName("Primitive");
        wrapper1.setServletClass("PrimitiveServlet");

        SimpleWrapper wrapper2 = new SimpleWrapper();
        wrapper2.setName("Modern");
        wrapper2.setServletClass("ModernServlet");

        container.addChild(wrapper1);
        container.addChild(wrapper2);
        container.addServletMapping("/Primitive", "Primitive");
        container.addServletMapping("/Modern", "Modern");

        Loader loader = new WebappLoader();
        container.setLoader(loader);

        LifecycleListener lifecycleListener = new SimpleContextConfig();
        container.addLifecycleListener(lifecycleListener);

        //和安全有关
        SecurityCollection securityCollection = new SecurityCollection();
        securityCollection.addPattern("/");
        securityCollection.addMethod("GET");

        SecurityConstraint securityConstraint = new SecurityConstraint();
        securityConstraint.addCollection(securityCollection);
        securityConstraint.addAuthRole("manager");

        LoginConfig loginConfig = new LoginConfig();
        loginConfig.setRealmName("Simple User Database Realm");

        Realm realm = new SimpleUserDatabaseRealm();
        ((SimpleUserDatabaseRealm)realm).createDatabase("conf/tomcat-users.xml");

        container.addConstraint(securityConstraint);
        container.setLoginConfig(loginConfig);
        container.setRealm(realm);
        return container;
    }
}
